//
//  GestureLockViewController.swift
//  SYDrive
//
//  Created by CleverPeng on 2020/7/2.
//  Copyright © 2020 cmss. All rights reserved.
//

import UIKit

class GestureLockViewController: BaseViewController, TKGestureLockBackDelegate {

    // 手势操作的一些回调, 不一定用的上
    var gestureLockDownBlock: (() -> ())?
    var unLockType: TKGestureLockViewUnlockType = TKGestureLockViewUnlockType.validate

    var backView: TKGestureLockBackView?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.loadNavigationItem()
        self.loadMainView()
    }

    func loadNavigationItem() {
        switch self.unLockType {
        case .create:
            self.navigationItem.title = "录入手势"
        case .validate:
            self.navigationItem.title = "验证手势"
        case .modify:
            self.navigationItem.title = "验证手势"
        case .update:
            self.navigationItem.title = "修改手势"
        case .delete:
            self.navigationItem.title = "关闭手势"
        default:
            self.navigationItem.title = "验证手势"
        }
    }

    func loadMainView() {

        self.view.backgroundColor = KDefine.Color.white

        let bgView = UIView()
        bgView.backgroundColor = UIColor.white
        self.view.addSubview(bgView)

        bgView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets.zero)
        }

        bgView.layer.cornerRadius = 4
        bgView.layer.masksToBounds = true

        let comView = UIView()
        comView.backgroundColor = UIColor.clear
        bgView.addSubview(comView)

        comView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 20, height: 20))
            make.left.equalToSuperview()
            make.bottom.equalTo(bgView.snp_bottomMargin)
        }

        self.view.layoutIfNeeded()
        Logger.printLog(comView.frame)

        backView = TKGestureLockBackView(frame: CGRect(x: 0, y: 0, width: bgView.bounds.size.width, height: comView.frame.origin.y + comView.frame.size.height), withUnLock: self.unLockType)
        backView?.delegate = self
        bgView.addSubview(backView!)

        backView?.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalTo(bgView.snp_bottomMargin)
        }
    }

    // MARK: - delegate
    func gestureLockBackView(_ backView: TKGestureLockBackView!, drawResult result: Bool) {
        switch self.unLockType {
        case .create:
            if result {
                TKGestureLockManager.sharedInstance()?.updateGestureLock(true)
                self.gestureLockDownBlock?()
            }
            self.backAction()
        case .modify:
            self.navigationItem.title = "修改手势"
            self.unLockType = TKGestureLockViewUnlockType.update
        case .update:
            if result {
                self.gestureLockDownBlock?()
            }
            self.backAction()
        case .delete:
            if result {
                self.gestureLockDownBlock?()
                self.backAction()
            }
        default:
            break
        }
    }

    func backAction() {
        self.navigationController?.popViewController(animated: true)
    }
}
